package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.GetMsgResponse;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ASUS on 2017/9/18.
 */

public class MsgListAdapter extends BaseAdapter {
    private ArrayList<GetMsgResponse> getMsgResponses;
    private Context mContext;

    public MsgListAdapter(ArrayList<GetMsgResponse> getMsgResponse, Context context) {
        this.getMsgResponses = getMsgResponse;
        mContext = context;
    }

    public ArrayList<GetMsgResponse> getGetMsgResponses(){
        return getMsgResponses;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_msg_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.time.setText(getMsgResponses.get(i).getCreate_time());
        holder.title.setText(getMsgResponses.get(i).getTitle());
        if(getMsgResponses.get(i).getContent() == null || getMsgResponses.get(i).getContent().equals("")){
            holder.content.setVisibility(View.GONE);
            holder.divider.setVisibility(View.GONE);
        } else {
            holder.content.setVisibility(View.VISIBLE);
            holder.divider.setVisibility(View.VISIBLE);
            String data=delectImgStyle(getMsgResponses.get(i).getContent());
            holder.content.setText(Html.fromHtml(replaceImgStyle(data)));
            /*String htmlData=getMsgResponses.get(i).getContent();
            String htmlData1=replaceImgStyle(htmlData);
            String imgStyle = "<style> img{ max-width:100%; height:auto;} a{color:#666666;text-decoration:none;background:#fff;}a:active{background:#fff;} a:focus{background:#fff;} a:hover{ background:#fff; color:#666666} </style>";
            htmlData1=imgStyle+htmlData1;
            holder.wv_webview.loadData(htmlData1, "text/html", null);*/
//            holder.content.setText(getMsgResponses.get(i).getContent());
        }

        return view;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        return getMsgResponses.size();
    }

    static class ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.divider)
        View divider;
        @BindView(R.id.content)
        TextView content;
        @BindView(R.id.wv_webview)
        WebView wv_webview;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    /**
     * 使用正则表达式 把html标签中的style属性全部替换成""
     */
    private String replaceImgStyle(String html) {
        String reg = "style=\"([^\"]+)\"";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(html);
        return matcher.replaceAll("");
    }
    //匹配img标签的正则表达式
    private String delectImgStyle(String result) {
        String regxpForImgTag =  "<\\s*img\\s+([^>]*)\\s*>";
        return result.replaceAll(regxpForImgTag,"");
    }

}
